package com.lizhao.win007com.match.webapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lizhao.win007com.commons.pojo.info.model.LeagueInfo;
import com.lizhao.win007com.commons.pojo.info.model.RateInfo;
import com.lizhao.win007com.commons.pojo.info.model.RelationInfo;
import com.lizhao.win007com.commons.pojo.match.model.Match;
import com.lizhao.win007com.commons.pojo.match.vo.MatchVO;
import com.lizhao.win007com.commons.pojo.match.vo.RelationInfoVO;
import com.lizhao.win007com.commons.restful.JsonPage;
import com.lizhao.win007com.info.service.IInfoService;
import com.lizhao.win007com.match.service.IMatchService;
import com.lizhao.win007com.match.webapi.mapper.MatchMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
@DubboService
public class MatchServiceImpl implements IMatchService {

    @Autowired
    private MatchMapper matchMapper;

    @DubboReference
    private IInfoService dubboInfoService;

    //获取联赛数据
    @Override
    public JsonPage<Match> getMatchInfoByTableAndCompanyIdAndPageAndPageSize(String table,Integer companyid,Integer page,Integer pageSize) {
        // 这行代码就是利用PageHepler框架指定分页查询参数的指令
        // 这行代码生效后,下一次运行的数据库查询会自动按照分页参数计算limit的数值,添加到sql语句之后
        PageHelper.startPage(page,pageSize);
        List<Match> list = matchMapper.findList(table,companyid);

        return JsonPage.restPage(new PageInfo<>(list));

    }

    //通过id删除数据
    @Override
    public void deleteByTableAndId(String table, Integer id) {
        matchMapper.deleteByTableAndId(table,id);
    }

    //获取总记录数
    @Override
    public Integer getTotal(String table) {
        List<String> tables = new ArrayList();
        tables.add("companyinfo");
        tables.add("config");
        tables.add("leagueinfo");
        tables.add("seasoninfo");
        tables.add("seasonmatch");
        tables.add("seasonmatch-one");
        tables.add("seasonmatch-two");
        tables.add("seasonmatch-three");
        tables.add("seasonmatch-repeat");
        tables.add("typeinfo");

        //如果存在表名，则返回具体总数，否则返回0
        if(tables.contains(table)){
            return matchMapper.findTotal(table);
        }

        return 0;
    }

    @Override
    public List<RelationInfoVO> getMatchByLeagueIdAndSeasonAndCompanyId(Integer leagueid, String season, Integer companyid) {
        Map<String, String> relationInfo = new HashMap<>();
        relationInfo.put("C>B>A13", "rate3");
        relationInfo.put("C>A>B31", "rate3");
        relationInfo.put("A>B>C10", "rate0");
        relationInfo.put("A>C>B01", "rate0");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //今天日期
        String today = sdf.format(new Date());

        //明天日期
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(calendar.DATE,1);//正数代表+几天
        String tomorrow= sdf.format(calendar.getTime());

        Map<Integer, String> mapLeagueList = new HashMap<>();
        List<LeagueInfo> leagueInfo = dubboInfoService.getAllLeagueInfo();
        for (LeagueInfo league : leagueInfo){
            mapLeagueList.put(league.getLeagueid(), league.getLeaguetype());
        }

        String table_one = "leaguematch-" + leagueid + "-one";
        Map<Integer, BigDecimal> asianInfo = new HashMap<>();
        List<Match> matchInfo = matchMapper.findList(table_one,companyid);
        for (Match match : matchInfo){
            asianInfo.put(match.getPageid(), match.getOdds());
        }


        //从当前胜率表获取胜率
        String table_relation = "leaguematch-relation-" + leagueid;
        List<RelationInfoVO> relationInfoList = new ArrayList<>();

        for (Map.Entry entry : relationInfo.entrySet()){
            String relation = (String) entry.getKey();
            String ratetag = (String) entry.getValue();
            List<RateInfo> rateInfoList = dubboInfoService.getRateByLeagueIdAndRelationAndRateTag(leagueid, relation, ratetag);

            if (rateInfoList.size() != 0){
                for (RateInfo rateInfo : rateInfoList){
                    Integer companyid1 = rateInfo.getCompanyid1();
                    Integer companyid2 = rateInfo.getCompanyid2();

                    String lastCaiInfo = "";
                    List<RelationInfo> currentMatchInfo = new ArrayList<>();

                    List<RelationInfo> relustInfo = matchMapper.findRelationByLeagueIdAndCompanyIdAndSeasonAndRelation(
                            table_relation, companyid1, companyid2, season, relation);

                    if (relustInfo.size() != 0){
                        for (RelationInfo result : relustInfo){
                            if (result.getCai() != ""){
                                lastCaiInfo += result.getCai();
                            }
                        }

                        //添加赛事到投注推荐表
                        for (RelationInfo result : relustInfo){
                            String matchdate = result.getMatchdatetime().substring(0,10);
                            if ((matchdate.equals(today) || matchdate.equals(tomorrow)) && result.getCai().length() == 0){
                                RelationInfoVO infoVO = new RelationInfoVO();
                                BeanUtils.copyProperties(result, infoVO);
                                infoVO.setLeagueid(leagueid);
                                infoVO.setLeaguetype(mapLeagueList.get(leagueid));
                                infoVO.setHistorycai(lastCaiInfo);
                                String who = result.getBegin1().compareTo(result.getEnd1()) == -1
                                        || result.getBegin1().compareTo(result.getEnd1()) == 1 ? "主" : "客";
                                infoVO.setWho(who);
                                infoVO.setGroup(companyid1 + "-" + companyid2);
                                infoVO.setP(result.getP1() + result.getP2() + result.getP3());
                                infoVO.setRelation(result.getRelationabs1());
                                infoVO.setOdds(asianInfo.get(result.getPageid()));
                                relationInfoList.add(infoVO);
                            }
                        }
                    }
                }
            }
        }
        System.out.println(relationInfoList);
        return relationInfoList;
    }

    //获取对阵数据
    @Override
    public List<MatchVO> getOpposeByLeagueIdAndCompanyId(Integer leagueid, Integer companyid){
        String table1 = "leaguematch-" + leagueid + "-three";
        String table2 = "leaguematch-" + leagueid + "-one";

        Date date = new Date();
        String currentYear = String.format("%tY", date);//当前日期
        String lastYear = String.valueOf((Integer.parseInt(currentYear) - 1));
        String season = "'" + lastYear + '-' + currentYear + "'";

        //获取leagueinfo并放到一HashMap里
//        List<LeagueInfo> leagueInfoList = dubboInfoService.getAllLeagueInfo();
//        HashMap<Integer,String> mapLeagueInfo = new HashMap<>();
//        for (LeagueInfo league : leagueInfoList){
//            mapLeagueInfo.put(league.getLeagueid(),league.getLeaguetype());
//        }

        List<Match> matchList = matchMapper.findListBySeason(table1, companyid, season);
        List<MatchVO> matchVOList = new ArrayList<>();

        for (Match match : matchList){
            MatchVO info = new MatchVO();
            BeanUtils.copyProperties(match, info);
            Match asianInfo = matchMapper.findListByTableAndCompanyIdAndPageId(table2, companyid, match.getPageid());
            info.setLeagueid(leagueid);

            if(asianInfo == null){
                info.setO3(null);
                info.setO1(null);
                info.setO0(null);
            }else{
                info.setO3(asianInfo.getBegin());
                info.setO1(asianInfo.getOdds());
                info.setO0(asianInfo.getEnd());
            }

            matchVOList.add(info);
        }

        return matchVOList;
    }
}
